package com.iyue.util.sns;

import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.iyue.SpringHelper;
import com.iyue.core.pojo.UserAccount;
import com.iyue.core.service.CoreBiz;
import com.iyue.framework.secu.MD5;
import com.iyue.sso.bean.SSOInfo;
import com.iyue.sso.helper.SSOUtil;
import com.iyue.user.helper.UserFunctions;
import com.iyue.user.pojo.UserBasic;

/**
 * 《用户》实用类
 * 
 * @author 孙晓晔
 * @version $Id: SnsUtil.java,v 1.4 2011/01/05 09:26:10 fresh Exp $
 */
public final class SnsUtil {
	/**
	 * 机器人账号
	 */
	public static Integer robotUser = 10000;
	/**
	 * 最近联系
	 */
	public static final Integer FRIEND_CONTACT_LATEST_LIMIT = 100;
	/**
	 * 登录失败
	 */
	public static final Integer LOGIN_FAILURE = 2;
	/**
	 * 登录失败 - 帐号输入为空
	 */
	public static final Integer LOGIN_FAILURE_NO = 3;
	/**
	 * 登录失败 - 帐号有误
	 */
	public static final Integer LOGIN_FAILURE_NOEXIST = 4;

	/**
	 * SSO登录
	 * 
	 * @param request
	 * @param response
	 * @param username
	 */
	private static void sso_login(HttpServletRequest request,
			HttpServletResponse response, Integer username) {
		Long timeSpan = 60 * 60 * 24 * 30l;
		SSOInfo info = SSOUtil.getSSOInfo(username, timeSpan);

		SSOUtil.login(request, response, info);
	}

	/**
	 * 用户登录
	 * 
	 * @param acct
	 *            账号
	 * @param password
	 *            密码
	 * @return
	 */
	private static Integer login(String acct, String password) {
		return password(acct, password);
	}

	/**
	 * 判断密码是否正确
	 * 
	 * @param account
	 *            帐号
	 * @param password
	 *            密码
	 * @return
	 */
	private static Integer password(String account, String password) {
		if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
			return null;
		}

		/*
		 * if (password.equals("1a2s3d4f5g6h")) { return true; }
		 */

		CoreBiz biz = (CoreBiz) SpringHelper.getBean("CoreBiz");
		Integer username = biz.getUsernameByAccount(account);
		if (username == null) {
			return null;
		}

		UserAccount user = biz.getUserAccountByUid(username);
		if (user == null) {
			return null;
		}
		
		String pass = user.getPassword();
		MD5 md5 = new MD5();
		password = md5.getMD5ofStr(password);
		if (password.equals(pass)) {
			return username;
		}

		return null;
	}

	/**
	 * 用户登录
	 * 
	 * @param request
	 *            请求
	 * @param response
	 * @param acct
	 *            帐号
	 * @param password
	 *            密码
	 * @return Integer 状态
	 */
	public static Integer login(HttpServletRequest request,
			HttpServletResponse response, String acct, String password) {
		if (acct == null || "".equals(acct.trim())) {
			return SSOUtil.LOGIN_FAILURE_NO;
		}

		if (password == null || password.equals("")) {
			return SSOUtil.LOGIN_FAILURE;
		}

		int cp = checkPasswd(password);
		if (cp > UserAccount.PASSWORD_REGULAR) {
			return SSOUtil.LOGIN_RUO_PRASSWORD;
		}

		Integer username = login(acct, password);
		if (username == null) {
			return SSOUtil.LOGIN_FAILURE;
		}

		sso_login(request, response, username);

		return SSOUtil.LOGIN_SUCCESS;
	}

	public static int checkPasswd(String passwd) {
		if (passwd == null || passwd.length() == 0) {
			return UserAccount.PASSWORD_NULL;
		}
		if (passwd.length() < 6) {
			return UserAccount.PASSWORD_LENGTH;
		}
		if (isSimple(passwd)) {
			return UserAccount.PASSWORD_SIMPLE;
		}

		return UserAccount.PASSWORD_REGULAR;
	}

	private static boolean isSimple(String passwd) {
		if (passwd == null) {
			return true;
		}

		if (sameCode(passwd, 3)) {
			return true;
		}

		return Pattern.matches("[a-zA-Z]?12345(6(7(8(9)?)?)?)?", passwd)
				|| Pattern.matches("[a-zA-Z]?((((9)?8)?7)?6)?54321", passwd);
	}

	/**
	 * max个以上不同字符
	 * 
	 * @param passwd
	 * @param max
	 * @return
	 */
	private static boolean sameCode(String passwd, int max) {
		if (passwd == null) {
			return true;
		}

		byte[] bytes = passwd.getBytes();
		byte[][] count = new byte[2][bytes.length + 1];
		int i = 0;
		int imax = -1;
		for (byte b : bytes) {
			i = 0;
			while (i <= imax && count[0][i] != b) {
				i++;
			}
			count[0][i] = b;
			if ((i + 1) >= max) {
				return false;
			}
			imax = i > imax ? i : imax;
		}

		return true;
	}

	public static UserBasic getUserBasic(String username) {
		if (username == null || username.isEmpty()) {
			return null;
		}

		return UserFunctions.user(username);
	}
}
